package com.ts.api.common.localcache;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.IteratorUtils;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.cache.local.Cache;
import com.gitee.apanlh.util.cache.local.CacheUtils;
import com.gitee.apanlh.util.log.Log;
import com.ts.api.module.common.entity.DistrictPO;
import com.ts.api.module.common.service.DistrictService;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 *  本地地区缓存
 *
 *  @author Pan
 */
@Component
public class DistrictCache {

    /** H5地区编码缓存Key */
    public static final String H5_DISTRICT_FORM_KEY = "h5_districtForm";
    /** APP地区编码缓存Key */
    public static final String APP_DISTRICT_FORM_KEY = "app_districtForm";

    private static final Cache<String, DistrictPO> CODE_CACHE = CacheUtils.cache(MapUtils.newHashMap());
    private static final Cache<String, DistrictPO> NAME_CACHE = CacheUtils.cache(MapUtils.newHashMap());
    private static final Cache<String, DistrictPO> SHORT_NAME_CACHE = CacheUtils.cache(MapUtils.newHashMap());

    private DistrictService districtService;

    public DistrictCache(DistrictService districtService) {
        this.districtService = districtService;
        //  初始化数据
        initData();
    }

    public void initData() {
        List<DistrictPO> list = districtService.list();
        //  转换省份Map
        Map<String, DistrictPO> provinceMap = MapUtils.newHashMap(newMap -> IteratorUtils.array(list, t -> Eq.call(t.getType(), "1", () -> newMap.put(t.getCode(), t))));
        //  剔除省
        CollUtils.filter(list, t -> t.getType().equals("1"));
        //  剔除非县级市的类型，县级/区/州/村/镇
        CollUtils.filter(list, t -> Eq.str(t.getType(), "3") && !t.getName().endsWith("市"));

        //  添加缓存
        IteratorUtils.array(list, t -> {
            //  设置城市归属省份
            String provinceCode = t.getPCode().substring(0, 2) + "0000";
            DistrictPO provinceDistrictPO = provinceMap.get(provinceCode);
            t.setProvinceCode(provinceDistrictPO.getCode());
            t.setProvinceName(provinceDistrictPO.getName());

            CODE_CACHE.put(t.getCode(), t);
            NAME_CACHE.put(t.getName(), t);
            SHORT_NAME_CACHE.put(t.getShortName(), t);
        });

        Log.get().info("初始化[地区][地市]数据成功");
        //  添加定制渠道-没有城市时使用
        DistrictPO districtPO = new DistrictPO();
        districtPO.setName("定制渠道");
        NAME_CACHE.put("定制渠道", districtPO);
    }

    /**
     *  根据城市编码获取城市实体类
     *
     *  @param  code    城市编码
     *  @return DistrictPO
     */
    public static DistrictPO getByCode(String code) {
        return CODE_CACHE.get(code);
    }

    /**
     *  根据城市名称，获取地区实体类
     *  可根据短名称或全名称
     *
     *  @param  cityName    城市名称
     *  @return DistrictPO
     */
    public static DistrictPO getByName(String cityName) {
        DistrictPO districtPO = NAME_CACHE.get(cityName);
        if (districtPO == null) {
            return SHORT_NAME_CACHE.get(cityName);
        }
        return districtPO;
    }
}
